<?php
/**
 * 深度垂直作者管理
 * act操作说明
 * list           作者列表
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . "includes/fckeditor/fckeditor.php");
require_once(ROOT_PATH . 'includes/cls_image.php');
require_once(ROOT_PATH . 'includes/lib_tvd.php');

/* 允许上传的文件类型 */
$allow_file_types = '|GIF|JPG|PNG|BMP|SWF|DOC|XLS|PPT|MID|WAV|ZIP|RAR|PDF|CHM|RM|TXT|';
/*------------------------------------------------------ */
//-- 作者列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    /* 取得过滤条件 */
    $filter = array();
    $smarty->assign('ur_here',      $_LANG['07_tvd_author_list']);
    $smarty->assign('action_link',  array('text' => '添加作者', 'href' => 'tvdAuthor.php?act=add'));
    $smarty->assign('full_page',    1);
    $smarty->assign('filter',       $filter);

    $authorList = getTvdAuthorList();

    $smarty->assign('authorList',      $authorList['arr']);
    $smarty->assign('filter',          $authorList['filter']);
    $smarty->assign('record_count',    $authorList['record_count']);
    $smarty->assign('page_count',      $authorList['page_count']);

    $sort_flag  = sort_flag($authorList['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    assign_query_info();
    $smarty->display('tvd/authorList.htm');
}

/*------------------------------------------------------ */
//-- 查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $authorList = getTvdAuthorList();
    $smarty->assign('authorList',      $authorList['arr']);
    $smarty->assign('filter',          $authorList['filter']);
    $smarty->assign('record_count',    $authorList['record_count']);
    $smarty->assign('page_count',      $authorList['page_count']);
    make_json_result($smarty->fetch('tvd/authorList.htm'));
}

/*------------------------------------------------------ */
//-- 添加文章
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('tvd_author_manage');

    $smarty->assign('ur_here',     '添加深度垂直作者');
    $smarty->assign('action_link', array('text' => '作者列表', 'href' => 'tvdAuthor.php?act=list'));
    $smarty->assign('form_action', 'insert');

    assign_query_info();
    $smarty->display('tvd/authorInfo.htm');
}

/*------------------------------------------------------ */
//-- 添加作者
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    admin_priv('tvd_author_manage');
    if (empty($_POST['author_name'])) {
        sys_msg('请输入作者名，不能为空');
    } else {
        $author_name = trim($_POST['author_name']);
    }
    /* 取得头像图片地址 */
    $head = '';
    if ((isset($_FILES['head']['error']) && $_FILES['head']['error'] == 0) || (!isset($_FILES['head']['error']) && isset($_FILES['head']['tmp_name']) && $_FILES['head']['tmp_name'] != 'none'))
    {
        // 检查文件格式
        if (!check_file_type($_FILES['head']['tmp_name'], $_FILES['head']['name'], $allow_file_types))
        {
            sys_msg($_LANG['invalid_file']);
        }

        // 临时文件
        $temp = ROOT_PATH . 'temp/img/'.FTP()->rName($_FILES['head']['name']);
        // 转移至临时文件
        $temp = @move_uploaded_file($_FILES["head"]["tmp_name"], $temp);
        if (!empty($temp)) {
            // 上传
            $remotePathName = 'images/tvd/head/' . FTP()->rName($_FILES['head']['name']);
            $head = FTP()->url . FTP()->upload($file2, $remotePathName);
            if (empty($head)) {
                $head = '';
            }
        }
        else {
            $head = '';
        }
    }
    /* 插入作者表 */
    $sql = "INSERT INTO egg_tvd_author(author_name, author_head) VALUES ('$author_name', '$head')";
    DB()->query($sql);
    $link[0]['text'] = '返回作者列表';
    $link[0]['href'] = 'tvdAuthor.php?act=list';
    $link[1]['text'] = '继续添加作者';
    $link[1]['href'] = 'tvdAuthor.php?act=add';
    sys_msg($_LANG['add'] . "&nbsp;" .$_POST['author_name'] . "&nbsp;" . $_LANG['attradd_succed'],0, $link);
}

/*------------------------------------------------------ */
//-- 编辑
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('tvd_author_manage');

    /* 取文章数据 */
    $sql = "SELECT * FROM egg_tvd_author WHERE author_id='$_REQUEST[id]'";
    $author = $db->GetRow($sql);

    $smarty->assign('author',     $author);
    $smarty->assign('ur_here',     $_LANG['tvd_edit']);
    $smarty->assign('action_link', array('text' => $_LANG['05_tvd_list'], 'href' => 'tvd.php?act=list&' . list_link_postfix()));
    $smarty->assign('form_action', 'update');

    assign_query_info();
    $smarty->display('tvd/authorInfo.htm');
}


/*------------------------------------------------------ */
//-- 修改作者
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'update')
{
    /* 权限判断 */
    admin_priv('tvd_author_manage');
    if (empty($_POST['author_name'])) {
        sys_msg('请输入作者名，不能为空');
    } else {
        $author_name = trim($_POST['author_name']);
    }
    if (empty($_POST['id'])) {
        sys_msg('数据出错，请联系技术大哥');
    } else {
        $id = intval($_POST['id']);
    }
    /* 取得头像图片地址 */
    $head = '';
    if ((isset($_FILES['head']['error']) && $_FILES['head']['error'] == 0) || (!isset($_FILES['head']['error']) && isset($_FILES['head']['tmp_name']) && $_FILES['head']['tmp_name'] != 'none'))
    {
        // 检查文件格式
        if (!check_file_type($_FILES['head']['tmp_name'], $_FILES['head']['name'], $allow_file_types))
        {
            sys_msg($_LANG['invalid_file']);
        }

        // 临时文件
        $temp = ROOT_PATH . 'temp/img/'.FTP()->rName($_FILES['head']['name']);
        // 转移至临时文件
        $isTemp = @move_uploaded_file($_FILES["head"]["tmp_name"], $temp);
        if (!empty($isTemp)) {
            // 上传
            $remotePathName = 'images/tvd/head/' . FTP()->rName($_FILES['head']['name']);
            $head = FTP()->url . FTP()->upload($temp, $remotePathName);
            if (empty($head)) {
                $head = '';
            }
        }
        else {
            $head = '';
        }
    }
    /* 插入作者表 */
    $sql = "UPDATE egg_tvd_author SET author_name = '{$author_name}', author_head = '{$head}' WHERE author_id = '$id'";
    DB()->query($sql);
    $link[0]['text'] = '返回作者列表';
    $link[0]['href'] = 'tvdAuthor.php?act=list';
    sys_msg($_LANG['add'] . "&nbsp;" .$_POST['author_name'] . "&nbsp;" . $_LANG['attradd_succed'],0, $link);
}

/*------------------------------------------------------ */
//-- 删除作者
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
    check_authz_json('tvd_author_manage');

    $id = intval($_GET['id']);

    /* 非空的分类不允许删除 */
    $sql = "SELECT COUNT(*) FROM ".$ecs->table('tvd')." WHERE author_id = '$id'";
    if ($db->getOne($sql) > 0)
    {
        make_json_error(sprintf('此作者下有文章，请勿删除'));
    }
    else{
        $sql = "DELETE FROM egg_tvd_author WHERE author_id = '$id'";
        DB()->query($sql);
    }

    $url = 'tvdAuthor.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;
}
/* 获得作者列表 */
function getTvdAuthorList()
{
    $filter = array();

    /* 作者总数 */
    $sql = 'SELECT COUNT(*) FROM egg_tvd_author';
    $filter['record_count'] = $GLOBALS['db']->getOne($sql);

    $filter = page_and_size($filter);

    /* 获取文章数据 */
    $sql = 'SELECT * FROM egg_tvd_author ';

    $arr = array();
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $rows;
    }
    return array('arr' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}
/* 上传文件 */
function upload_head_file($upload)
{
    if (!make_dir("../images/tvd"))
    {
        /* 创建目录失败 */
        return false;
    }

    $filename = cls_image::random_filename() . substr($upload['name'], strpos($upload['name'], '.'));
    $path     = ROOT_PATH. "images/tvd/" . $filename;

    if (move_upload_file($upload['tmp_name'], $path))
    {
        return "images/tvd/" . $filename;
    }
    else
    {
        return false;
    }
}